Repository navigation
Conversation
… fieldless enum
A newtype is a struct with exactly one unnamed field. A derived newtype
encrypts exactly as its inner value does. A fieldless enum encrypts as
one u32 leaf.
But the derive implemented no Scalar marker for either. So a caller that
bounds on Scalar refused `struct Email(String)`. One such caller is
keyset.encrypt in stack-encrypt. The user had to write
`impl Scalar for &Email {}` by hand.
The derive now implements Scalar for these types, by value and by
reference. Each impl for a newtype has a bound: the inner type is a
Scalar. So `Tags(Vec<String>)` and `Nickname(Option<String>)` still
derive Encrypt, for use as a field. Neither type is a Scalar. A struct
with fields and a recursive newtype get no impl.
The compiler checks a where clause that names no generic parameter
where the impl is written. A false clause of that kind is an error. So
the borrowed impl's bound names the borrow's lifetime. The owned impl's
bound is inside a `for<'__s>` binder. The compiler then checks each
bound only where code asks for the Scalar impl.
A Scalar impl that a user wrote for such a type now conflicts with the
derived impl (E0119). The CHANGELOGs tell the user to delete it.
…r a refused type The derive now implements Scalar for a newtype over one value. So `keyset.encrypt(Email(..))` compiles with no other code. The newtype's leaf has the same plaintext and length as the leaf of its inner String. A new test opens both leaves with the stored data keys to show this. The test also checks that `keyset.decrypt::<String>` opens what Email encrypted. For a refused type, the error used to name the bound `for<'a> &'a T: vitaminc_aead::Scalar`. A Stack Encrypt user does not know that trait. OpaqueEncode now has an on_unimplemented message. The message says that the value cannot be encrypted into one Ciphertext, and what keyset.encrypt encrypts. The message also names what to use instead: EncryptInto, encrypt_tree or OpaqueBlob. The blanket impl has `#[diagnostic::do_not_recommend]`. So the compiler reports the refusal against OpaqueEncode, not against the Scalar or Zeroize bound below it. Without the attribute, the compiler shows the message of the bound instead. Anyone can implement Scalar. So seal now checks, in an inline const, that the DEPTH constant of the type.s borrow is depth::SCALAR. A false Scalar impl fails `cargo build` with E0080. `cargo check` does not run the check. A compile_fail doctest tests it. The existing compile-fail snapshots change only their wording. Each refusal is still E0277 at the same place in the source.
|
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
Mutation testing (cargo-mutants,
|
| caught | missed | unviable | timeout |
|---|---|---|---|
| 11 | 0 | 2 | 0 |
Every mutant in the changed lines was caught by a test.
coderdan
left a comment
There was a problem hiding this comment.
Code review at medium effort. Seven findings: three important, four nits.
I reproduced the three important ones by compiling a small test against this branch. The four nits are from reading the code.
| cipher: &'a KeysetCipher<'_, K>, | ||
| context: ContextPiece<'static>, | ||
| ) -> Pending<'a, Ciphertext, K> { | ||
| const { |
There was a problem hiding this comment.
Important: a hand-written one-value type that leaves DEPTH at its default no longer builds.
This check asserts depth::is_scalar(<&S>::DEPTH), and is_scalar(None) is false. Encrypt::DEPTH defaults to None, and its docs say a hand-written impl may leave it at None.
Reproduced: a type with impl Encrypt for &Token (no DEPTH) and impl Scalar for &Token {} compiled with keyset.encrypt before this PR. Now cargo build fails with E0080. cargo check still passes.
The CHANGELOG lists the check under Fixed and describes only the container case.
Fix, one of:
- keep the check strict, move the entry to Breaking, and say that a hand-written
Scalartype must setDEPTH = depth::SCALAR; - accept
None, and refuse only a depth above 0. This is weaker: a false marker that leavesDEPTHunset passes.
There was a problem hiding this comment.
Fixed in c3f2cf7 and 45ea2c5. The check stays strict, because a Scalar marker on a type whose DEPTH says "depends on the value" contradicts itself.
- The assertion message now names the fix: "Set
const DEPTH: Option<usize> = depth::SCALAR;in theEncryptimpl of its borrow." - The
OpaqueEncodedocs and theScalardocs say the same. - The stack-encrypt CHANGELOG lists it under Breaking, with the line to add. The old bullet under Fixed is gone.
Every hand-written Scalar impl in this repository already sets DEPTH, so none needed a change. I re-ran the reproduction: it still fails the build, now with the new message.
There was a problem hiding this comment.
minor The new message misdirects for a derived newtype over a hand-written type. With impl Encrypt for &Inner (no DEPTH) and impl Scalar for &Inner {}, #[derive(Encrypt, Zeroize)] struct Outer(Inner) inherits both, and keyset.encrypt(Outer(..)) fails with E0080 telling the user to set DEPTH in Outer's borrow impl, which the macro wrote (scalar.rs:304-306; reproduced). Say that for a derived newtype the impl to fix is the innermost hand-written type's.
Source: code-review · claude-opus-5-5
Generated by Claude Code
There was a problem hiding this comment.
Reproduced and fixed in 52c2a01. A const panic cannot name a type on stable Rust, so the message says where to look:
a type marked
Scalarmust encrypt as one value. Setconst DEPTH: Option<usize> = depth::SCALAR;in theEncryptimpl that you wrote for its borrow. For a newtype that derivesEncrypt, set it on the innermost type whoseEncryptimpl you wrote by hand.
The OpaqueEncode docs and the Breaking entry say the same.
| .predicates | ||
| .push(parse_quote!(&'__r #ty: #krate::Scalar)); | ||
| } | ||
| Shape::Enum(_) => {} |
There was a problem hiding this comment.
Important: a fieldless enum marked #[aead(recursive)] gets a Scalar impl that the new build check then refuses.
scalar() skips a recursive newtype, but this arm ignores recursive. depth() writes no DEPTH for any recursive type, so the enum's DEPTH stays None.
Reproduced: #[derive(Encrypt, Zeroize)] #[aead(recursive)] enum Status { Active }, then keyset.encrypt(Status::Active). cargo build fails with E0080, about a Scalar impl the user did not write.
Fix, one of:
- write
depth::SCALARfor a fieldless enum even when it is marked recursive, because a fieldless enum cannot contain itself; - write no
Scalarimpl for a recursive enum; - refuse
recursiveon a fieldless enum.
There was a problem hiding this comment.
Fixed in c2b8fe8. A fieldless enum has no fields, so it cannot contain itself. The derive now ignores recursive on one, and writes DEPTH = depth::SCALAR and the u32 FIXED as it does without the attribute.
I chose this over refusing the attribute, which would break code that compiles today. The attribute docs say it has no effect on a fieldless enum.
A derive test and a stack-encrypt test cover it. I re-ran the reproduction, and it now builds.
| owned | ||
| .make_where_clause() | ||
| .predicates | ||
| .push(parse_quote!(for<'__s> #ty: #krate::Scalar)); |
There was a problem hiding this comment.
Important: the CHANGELOG's migration advice is wrong for a newtype over a type that is not Scalar.
The derive always writes impl Scalar for &Name where &Inner: Scalar. Rust treats that impl as overlapping a hand-written impl Scalar for &Name {} even when the bound is false. Reproduced with struct Tags(Vec<String>): E0119.
The CHANGELOG says "After you delete the impl, keyset.encrypt still encrypts the type". That is true only when the inner type is a Scalar. For a newtype over, say, an OwnedFrame that the author checks holds one value, deleting the impl leaves a type that keyset.encrypt refuses.
Fix: say in the Breaking entry that this case needs a hand-written Encrypt impl (with DEPTH = depth::SCALAR) in place of the derive. An attribute that turns the derived marker off is the alternative.
There was a problem hiding this comment.
Fixed in 45ea2c5. The three CHANGELOGs now give the migration for each case:
- For a fieldless enum, or a newtype whose inner type is one value: delete the hand-written impl.
- For a newtype over another type, whose value the author knows is one leaf: stop deriving
Encrypt. WriteEncryptby hand for the type and its borrow, withconst DEPTH: Option<usize> = depth::SCALAR;, and keep theScalarimpl.
They also say that the conflict (E0119) appears even when the inner type is not a Scalar. There is no opt-out attribute.
| /// ``` | ||
| #[diagnostic::on_unimplemented( | ||
| message = "`{Self}` cannot be encrypted into one `Ciphertext`", | ||
| label = "`keyset.encrypt` encrypts one owned value", |
There was a problem hiding this comment.
Nit: the message misleads when the call is not keyset.encrypt, or when the missing bound is not Scalar.
vec![1u8, 2].encrypt_into::<Vec<Ciphertext>, _>(keyset)gets akeyset.encryptlabel, and a note that says to useEncryptInto, which the caller already uses.keyset.encrypt(OpaqueBlob::new(value))with a value that is notZeroizeis told to wrap the value inOpaqueBlob.#[derive(Encrypt)] struct Email(String);with noZeroizeis told that a newtype over aString"is one value too", in the error that refuses it.
Fix: make the label neutral about the call, for example "this must be one owned value". Consider whether the Zeroize cause can stay visible for a type that is already a Scalar.
There was a problem hiding this comment.
Fixed in c3f2cf7.
- The label is now neutral: "this must be one owned value that can be wiped".
- The notes list three conditions (one scalar, owned,
Zeroize), then describe the other calls without telling the reader to use one. - A missing
Zeroizeis now named. TheScalarbound moved into a sealed helper trait, and only that impl hasdo_not_recommend. So#[derive(Encrypt)] struct Email(String);with noZeroizegets "required forEmailto implementZeroize".
One thing to know: for a type that is not one value, the help line names the helper by its full path, stack_encrypt::scalar::sealed::OneValue. It is in the review notes.
There was a problem hiding this comment.
minor Still present at 369b17bc for OpaqueBlob<Unwiped> (tests/ui/opaque_blob_requires_zeroize.stderr): the notes say the value "must be one scalar" and that "OpaqueBlob encrypts a whole Serialize value", to a caller already using OpaqueBlob whose only gap is Zeroize. Cover OpaqueBlob<T> in the first condition, for example "or an OpaqueBlob<T> whose T is Serialize + Zeroize".
Source: mattpocock-skills:code-review (spec) · claude-opus-5-5
Generated by Claude Code
There was a problem hiding this comment.
Fixed in 390747a. The first condition now reads "the value must be one scalar or an OpaqueBlob<T>", and the Zeroize condition adds: "For an OpaqueBlob<T>, T must implement Zeroize and Serialize." The last note now describes the other calls, so it reads correctly for a caller who already uses OpaqueBlob. The six snapshots are re-recorded, with the same error headers and spans.
| #[diagnostic::on_unimplemented( | ||
| message = "`{Self}` cannot be encrypted into one `Ciphertext`", | ||
| label = "`keyset.encrypt` encrypts one owned value", | ||
| note = "one value is a `String`, a `bool`, a float, a `Vec<u8>`, or an integer other than \ |
There was a problem hiding this comment.
Nit: "an integer other than u8" is too wide.
usize and isize are not Scalar. The list in vitaminc-aead covers i8 to i128 and u16 to u128. So keyset.encrypt(5usize) fails with a note that lists its own type as accepted.
Fix: "a fixed-width integer other than u8", as the trait docs say.
There was a problem hiding this comment.
Fixed in c3f2cf7. The note now says "a fixed-width integer other than u8", as the trait docs do.
| /// whose borrow is not one value. `cargo build` runs the check, and | ||
| /// `cargo check` does not. | ||
| /// | ||
| /// ```compile_fail,E0080 |
There was a problem hiding this comment.
Nit: this doctest does not prove why the build fails.
rustdoc checks the error code of a compile_fail test only on nightly. This repository pins stable, so any compile error in the snippet keeps the test green. A rename of depth::container or FakeDataKeySource would do that with the const check removed.
Fix: add a positive control. The same snippet with DEPTH = depth::SCALAR must compile as a passing doctest.
There was a problem hiding this comment.
Fixed in c3f2cf7. The docs now have two doctests, one after the other. The first passes: a hand-written leaf with DEPTH = depth::SCALAR. The second is the compile_fail one, and it differs from the first only in DEPTH. So a compile error from anything else would fail the first test.
| struct Wrapper<T: Zeroize>(T); | ||
|
|
||
| async fn encrypt(keyset: &KeysetCipher<'_, FakeDataKeySource>) { | ||
| let _: Ciphertext = keyset.encrypt(Email("ada@example.com".into())).await.unwrap(); |
There was a problem hiding this comment.
Nit: no stack-encrypt test covers a derived fieldless enum.
The CHANGELOG says keyset.encrypt encrypts a fieldless enum that derives Encrypt, as one u32. The pass test and tests/derived_newtype.rs use newtypes only.
Fix: add an enum that derives Encrypt and Zeroize to the pass test, and a round trip that decrypts it as u32.
There was a problem hiding this comment.
Fixed in c6b5ba3. The pass test has a fieldless enum, and one marked recursive. tests/derived_newtype.rs checks each variant against the u32 of its discriminant: the same leaf plaintext, the same length, and keyset.decrypt::<u32> returns the discriminant.
… one leaf The derive implements `Scalar` for every fieldless enum. But it wrote no `DEPTH` for a type marked `#[aead(recursive)]`, so such an enum kept the default `DEPTH` of `None`. `keyset.encrypt` checks that a `Scalar` type has a `DEPTH` of `depth::SCALAR`. So `keyset.encrypt(Status::Active)` failed the build with `E0080`, about an impl the user did not write. A fieldless enum has no fields, so it cannot contain itself. The derive now ignores `recursive` on a fieldless enum. The enum gets a `DEPTH` of `depth::SCALAR` and its `u32` `FIXED` type, as it does without the attribute. Refusing the attribute was the other option. It would break code that compiles today, for no gain.
…Zeroize cause in refusals `keyset.encrypt` checks, when the program is built, that a `Scalar` type's borrow has a `DEPTH` of `depth::SCALAR`. A hand-written `Encrypt` impl that left `DEPTH` at its default `None` used to compile with `keyset.encrypt`. It now fails the build. The check stays strict. `None` means that the depth depends on the value, and a `Scalar` impl says the opposite. The assertion message, the `OpaqueEncode` docs, and the `Scalar` docs now give the line to add: `const DEPTH: Option<usize> = depth::SCALAR;`. The `compile_fail` doctest of the check now has a passing twin. The two differ only in `DEPTH`. On stable, rustdoc does not check the error code of a `compile_fail` test, so the twin shows that the snippet compiles apart from the check. The refusal message no longer names the call, so it reads correctly for `encrypt_with_context` and `EncryptInto`. It lists the three conditions, then the other calls. It says "a fixed-width integer other than `u8`", since `usize` and `isize` are not scalars. The `Scalar` bound moved into a sealed helper trait, `OneValue`. The impl of `OneValue` has `do_not_recommend`, and the blanket impl of `OpaqueEncode` no longer has it. So a type that is not one value is still reported under the message of `OpaqueEncode`, without Vitamin C's `Scalar`. A newtype over a `String` that only lacks `Zeroize` now gets an error that names `Zeroize`. `OneValue` has `EncryptRef` as a supertrait, not the higher-ranked `Scalar` bound. So `seal` walks the value through `EncryptRef::lend`. The walk is the same as before.
The pass test and the runtime test covered only newtypes. A fieldless enum that derives `Encrypt` now encrypts with `keyset.encrypt`, so both tests cover it too. The runtime test checks that each variant seals the same leaf as the `u32` of its discriminant, and that `keyset.decrypt::<u32>` opens it. The runtime test also encrypts an enum marked `#[aead(recursive)]`. The test file is built, so it runs the build-time `DEPTH` check that failed for such an enum before the derive fix.
The Breaking entries said that after you delete a hand-written `Scalar` impl, `keyset.encrypt` still encrypts the type. That is false for a newtype over a type that is not a `Scalar`. The derived impl conflicts with a hand-written one even then (`E0119`), and deleting the impl leaves a type that `keyset.encrypt` refuses. The entries in the three CHANGELOGs now give both cases. For a fieldless enum, or a newtype over a `Scalar`, delete the impl. A newtype can wrap another type whose value the author knows is one leaf. For that newtype, stop deriving `Encrypt`. Write `Encrypt` by hand for the type and its borrow, with `DEPTH` set to `depth::SCALAR`. The stack-encrypt CHANGELOG moves the `DEPTH` check to Breaking, and says which line to add. The derive CHANGELOG says that a fieldless enum marked recursive is one leaf.
The blanket impl of `OpaqueEncode` had become one line, and rustfmt keeps an empty impl on one line. The open decrypt branch changes the line after this impl, so the two branches conflicted. The impl now has a comment in its body. So rustfmt writes the `where` clause on its own lines, and the two changes no longer touch adjacent lines. The comment says why the `Scalar` bound is in `OneValue`.
cipherstash-agent
left a comment
There was a problem hiding this comment.
Review attestation
Reviewed 369b17bc · posted from Claude Code · claude-opus-5-5 · effort medium
Sources: code-review · claude-opus-5-5; mattpocock-skills:code-review (standards and spec agents) · claude-opus-5-5; company-skills:rust-security agent · claude-opus-5-5
Skills: code-review, mattpocock-skills:code-review, company-skills:rust-security, company-skills:review-cross-check
Findings (8): 8 minor — 6 inline, 2 in reply.
The rust-security review found no security defects. Each finding was verified at 369b17bc and cross-checked against the existing threads; findings the PR's review notes already raise (the private OneValue path, the HRTB-wrapped owned marker, the cargo build-only check) are not repeated.
Generated by Claude Code
| So `struct Email(String)` needs no `Scalar` impl of its own. | ||
| A `Protected<Email>` is a `Scalar` too. | ||
| `struct Tags(Vec<String>)` still derives `Encrypt`, but it is not a `Scalar`. | ||
| A `Scalar` impl you wrote for such a type now conflicts with the derived impl, even when the inner type is not a `Scalar`. |
There was a problem hiding this comment.
minor This E0119 conflict is a breaking change, but it sits under ### Added in [Unreleased]. The vitaminc-aead-derive and stack-encrypt changelogs file it under ### Breaking, as this crate's 0.10.0 section does. Add ### Breaking to [Unreleased] and move the conflict and migration sentences (L50-53) there.
Source: code-review; mattpocock-skills:code-review (standards) · claude-opus-5-5
Generated by Claude Code
There was a problem hiding this comment.
Fixed in 09c47a1. [Unreleased] now has a ### Breaking section, as 0.10.0 does, and the conflict and both migration cases moved there. The Added entry points to it.
| `struct Email(String)` with `#[derive(Encrypt, Zeroize)]` now encrypts | ||
| with `keyset.encrypt(Email(..))`, with no other code. Before this | ||
| change, the newtype needed a hand-written `impl Scalar for &Email {}`. | ||
| That impl needed a dependency on the `Scalar` trait of `vitaminc-aead`. |
There was a problem hiding this comment.
minor "with no other code" and "That impl needed a dependency on ... vitaminc-aead" read as if the dependency is gone, but the derive still expands to ::vitaminc_aead:: paths (vitaminc-aead-derive/src/attrs.rs:41). A crate that depends only on stack-encrypt fails at the derive with E0433 cannot find vitaminc_aead. Say the crate still needs vitaminc-aead until #2013 lands, as src/scalar.rs:72-74 does.
Source: code-review; mattpocock-skills:code-review (standards) · claude-opus-5-5
Generated by Claude Code
There was a problem hiding this comment.
| note = "third, the type must implement `Zeroize`, so that the value can be wiped after it \ | ||
| is encrypted.", |
There was a problem hiding this comment.
minor A fieldless enum now passes keyset.encrypt, but #[derive(Zeroize)] on a fieldless enum is a no-op, so keyset.encrypt(Status::Suspended) leaves the discriminant in memory despite this note (reproduced with zeroize 1.9 and zeroize_derive 1.5). The same holds for a derived newtype over such an enum and for the Protected / OpaqueBlob drop wipe. Say that a fieldless enum's variant is not wiped, here and in the derive's # Enums docs (vitaminc-aead-derive/src/lib.rs), or keep enums out of keyset.encrypt.
Source: code-review · claude-opus-5-5
Generated by Claude Code
There was a problem hiding this comment.
Confirmed, and documented in 053cee4 and 1ba2012. With zeroize 1.9 and zeroize_derive 1.5, the derived impl is an empty match arm for each variant, and Status::Suspended is unchanged after .zeroize().
Enums stay accepted by keyset.encrypt. The OpaqueEncode docs, the derive's # Enums docs and the CHANGELOG now say that #[derive(Zeroize)] does not wipe a fieldless enum. They show the fix: a #[default] variant and impl zeroize::DefaultIsZeroes for Status {}, which makes zeroize() overwrite the value with the default variant. A doctest checks that the fix wipes the value.
The underlying gap is #2015, which also covers decrypt and enum fields in a record.
| /// | ||
| /// The newtype encrypts exactly as its inner value does. So | ||
| /// `keyset.decrypt::<String>` opens what an `Email` encrypted. The derive | ||
| /// comes from Vitamin C, so your crate needs `vitaminc-aead` as a |
There was a problem hiding this comment.
minor The hand-written path these docs and the doctests at L234-235 and L267-268 describe needs vitaminc_aead::Scalar and vitaminc_aead::plaintext::depth, and stack-encrypt re-exports neither (src/lib.rs:779-782). #2013 covers the derive's paths, not these. Re-export Scalar and depth from stack_encrypt and point the docs and CHANGELOG (L1293, L1301) at them.
Source: mattpocock-skills:code-review (spec) · claude-opus-5-5
Generated by Claude Code
There was a problem hiding this comment.
Not in this PR. A hand-written Encrypt impl also needs vitaminc_aead::plaintext::Sink, so re-exporting Scalar and depth would not complete that path: the user's crate would still list vitaminc-aead.
The whole surface belongs in one change, so I have added the hand-written path to #2013, which already covers the derive's paths.
| let name = &input.ident; | ||
| // A fieldless enum has no fields, so it cannot contain itself. It is one | ||
| // `u32` leaf, with a fixed depth, even when it is marked `recursive`. | ||
| let recursive = attrs.recursive && !matches!(shape, Shape::Enum(_)); |
There was a problem hiding this comment.
minor Possible Duplicated Code: the "one fixed-depth value" rule is now written four times, this override plus recursive || names_itself(..) in scalar (L163), depth (L216) and fixed (L310). Compute it once here, for example let one_value = !recursive && !names_itself(name, &shape);, and pass that, so a new shape cannot miss one site.
Source: mattpocock-skills:code-review (standards) · claude-opus-5-5
Generated by Claude Code
There was a problem hiding this comment.
Fixed in a47a1e7. The derive decides once whether a type has a fixed depth, and passes that to depth, fixed and scalar. The output is unchanged: the expansion of nine inputs, from a newtype to a recursive enum to an empty struct, is byte-identical before and after, and the 62 expansion tests pass without change.
| let mut owned = generics.clone(); | ||
| let mut by_ref = ref_generics.clone(); |
There was a problem hiding this comment.
minor scalar clones both generics before the match, and the recursive or self-naming newtype, map and empty arms return without using them. Move the clones into the Newtype and Enum arms. scalar, depth and fixed also take the same (krate, input, shape, recursive) group (possible Data Clumps).
Source: mattpocock-skills:code-review (standards) · claude-opus-5-5
Generated by Claude Code
There was a problem hiding this comment.
Fixed in a47a1e7. scalar clones the generics only in the Newtype and Enum arms. I left the shared argument list of the three functions as it is.
…d depth The rule "this type is one value of fixed depth" was written four times. The enum override had one copy. `scalar`, `depth` and `fixed` each had `recursive || names_itself(..)`. A new shape could miss one of them. The derive now computes `fixed_depth` once and passes it to the three functions. `scalar` now clones the generics only in the arms that use them. `fixed` no longer takes the input. It used the input only for the old check. The expansion does not change. The expansion tests pass unchanged. I also compared the token output of nine inputs before and after: - a newtype, and a generic newtype - a newtype marked recursive, and a newtype that names itself - an enum, and an enum marked recursive - a struct, a struct marked recursive, and an empty struct The two outputs are identical.
The conflict with a hand-written `Scalar` impl (E0119), and how to migrate, were listed under Added. They are a breaking change. The `[Unreleased]` section now has a Breaking section, as the 0.10.0 section has, and the entry is there. The Added entry points to it.
The Fixed entry said that a derived newtype encrypts "with no other code", and that the old impl "needed a dependency" on `vitaminc-aead`. Both read as if the dependency were gone. It is not: the derived code names `vitaminc_aead`, so the crate still lists `vitaminc-aead` (#2013). The entry now says only that the hand-written `Scalar` impl is gone, and that the crate still needs `vitaminc-aead` to use the derive.
`#[derive(Zeroize)]` from zeroize 1.9.1 (zeroize_derive 1.5.0) writes a `zeroize` for a fieldless enum whose match arms are all empty. I checked the expansion, and a test that calls `zeroize()` on `Status::Suspended`. The value is still `Suspended` afterwards. So `keyset.encrypt` cannot wipe the variant of such an enum from memory, although the docs said that the source is wiped. The `OpaqueEncode` docs, the `# Enums` docs of the derive, and the stack-encrypt CHANGELOG entry now say so. They show how to have the variant wiped: give the enum a `#[default]` variant and implement `zeroize::DefaultIsZeroes`. The `zeroize` crate then overwrites the value with the default variant, with a volatile write. A doctest in each crate checks that `zeroize()` writes the default variant. Enum support stays as it is.
… the DEPTH check A newtype that derives `Encrypt` copies the `DEPTH` of the type it wraps. Take `#[derive(Encrypt, Zeroize)] struct Outer(Inner)`, where `Inner` has a hand-written `Encrypt` impl with no `DEPTH`. Then `keyset.encrypt(Outer(..))` fails the build. The message told the user to set `DEPTH` in the impl for the borrow of `Outer`, which the derive wrote. The assertion message now says to set `DEPTH` in the impl that the user wrote. For a derived newtype, that is the innermost type with a hand-written `Encrypt` impl. The `OpaqueEncode` docs and the Breaking entry of the CHANGELOG say the same.
… must meet An `OpaqueBlob<T>` whose `T` is not `Zeroize` got a refusal that said the value must be one scalar. Its last note explained `OpaqueBlob` to a caller who already uses it. The only missing condition was `Zeroize`. The first condition now reads "one scalar or an `OpaqueBlob<T>`". The `Zeroize` condition adds that, for an `OpaqueBlob<T>`, `T` must implement `Zeroize` and `Serialize`. That matches the bound on the `OpaqueEncode` impl of `OpaqueBlob`. The last note now says what each call does for a value that is not one scalar. So it also reads correctly for a caller who already uses one of these calls. The ui snapshots change only in these notes. Each error is still E0277 at the same place in the source.
The CHANGELOG entry on fieldless enums says that `#[derive(Zeroize)]` does not wipe the variant. The gap is now tracked as #2015, so the entry points to it. The rustdoc stays as it is, without the issue number.
Brings in #2011, the run-time single-value API and the scalar bound on typed decrypt. Both pull requests changed the trybuild snapshots owned_collection_elements.stderr and opaque_blob_requires_zeroize.stderr. Git merged them without a conflict. Recording them again with TRYBUILD=overwrite on Rust 1.94.1 gave the same files. The encrypt errors are from this branch. The decrypt errors are from #2011.
Brings in #2014 and, through it, next with #2011. Conflicts: - stack-encrypt's lib.rs: this branch re-exports `FromScalar` and `Scalar`, and #2011 re-exports `ScalarTarget`. The merge re-exports all three. - stack-encrypt's CHANGELOG: both sides added an entry at the top of Added. The merge keeps both entries. - owned_collection_elements.stderr: the merge takes the snapshot of #2014. Recording it again with TRYBUILD=overwrite gave the same file. #2011 added tests and a doc example to vitaminc-aead that use the derives from vitaminc_aead. On this branch vitaminc-aead has no derives. So these now use stack_encrypt_derive with `#[aead(crate = "::vitaminc_aead")]`, as the other vitaminc-aead tests do: - tests/scalar_target.rs - four compile-fail tests in tests/ui/scalar_target/ - the example on `ScalarTarget` in typed.rs The four snapshots of those compile-fail tests change only in their line numbers. Two doc comments in typed.rs now say "Stack Encrypt's `#[derive(Decrypt)]`", as the rest of the file does.
The merge of #2014 added packages/mn-meta.err and packages/eql/tests/timing/mn-meta.err. Both are empty logs from a local lockfile check, and are not part of the change.
Summary
A newtype that derives
Encryptover one value now works withkeyset.encrypt, with no other code. Whenkeyset.encryptrefuses a type, the compiler error now says what to use instead, in Stack Encrypt's terms.Closes #2012.
Changes
#[derive(Encrypt)]on a newtype now also writes theScalarmarker thatkeyset.encryptasks for. The marker applies only when the inner type has it:struct Email(String)is accepted bykeyset.encrypt.struct Tags(Vec<String>)still derivesEncrypt, so it can be a field of a struct, andkeyset.encryptstill refuses it.Protected<Email>work the same way.#[aead(recursive)].OpaqueEncodehas a#[diagnostic::on_unimplemented]message. It lists the three conditions a type must meet, and describesEncryptInto,keyset.encrypt_tree, andOpaqueBlobfor the other cases. The error no longer names a vitaminc trait, and it still names a missingZeroize.sealchecks, in an inlineconst, that the type'sDEPTHreally is one value.cargo buildruns this check.cargo checkdoes not.OpaqueEncodedocs have a runnable example. Thestack-encrypt,vitaminc-aead, andvitaminc-aead-derivechangelogs have entries.Breaking changes
Scalarimpl on a type that derivesEncryptnow conflicts with the derived one (E0119). This applies to a newtype and to a fieldless enum.Encrypt. WriteEncryptby hand for the type and its borrow, and keep theScalarimpl.Scalartype must set itsDEPTH. Addconst DEPTH: Option<usize> = depth::SCALAR;to theEncryptimpl of the type's borrow. Without it,keyset.encryptfails the build.No code in this repository needed either change.
What the derive writes
For
struct Email(String), in addition to the twoEncryptimpls:For
struct Tags(Vec<String>)it writes the same two impls overVec<String>. Both compile, and neither applies, becauseVec<String>is not aScalar.The error for a refused type
A one-value type with no
Zeroizegets the same message. The message then has the line "required forEmailto implementZeroize".Verification
Run locally on Rust 1.94.1.
-D warnings, for all features, and forstack-encryptwith no default featurescargo test -p stack-encryptwith all features. This run includes the compile tests and doctests.cargo test -p stack-encryptwith no default featuresvitaminc-aead: tests under six feature sets: none,jiff,rust_decimal,uuid,hlist, and all.vitaminc-aead-derive: tests.-D warnings, for the three crates.--in-diff, the three crates): 8 mutants, 7 caught, 1 that does not compile, 0 missed.eql-bindingswith all features, and the encryption testsrecipher-js: clippy and testsStringand over ani64, with no hand-written markerVec, a newtype over anOption, and a struct with named fieldsEmailandStringseal the same leaf plaintext at the same lengthkeyset.decrypt::<String>opens whatEmailsealedu32of its discriminantDEPTHmisetasks. CI runs them.Related
keyset.decrypt. The two branches merge without conflict. Both change the snapshottests/ui/owned_collection_elements.stderr, so the second to merge re-records it.#[derive(Encrypt, Decrypt)]does not compile in a crate that depends only onstack-encrypt#2013: the derives do not compile in a crate that depends only onstack-encrypt. This PR's docs say, as is true today, that the user's crate needsvitaminc-aead.Review notes
Open questions:
impl Scalar for Tags where Vec<String>: Scalar {}is a hard error, because the bound is false and names no parameter. With a binder,where for<'__s> Vec<String>: Scalar, the compiler checks the bound only where the impl is used. The borrowed marker does not need this. It is the only markerkeyset.encryptasks for. The owned one is there so that a newtype matches the built-in types under a genericS: Scalarbound. The alternative is to write only the borrowed marker.stack_encrypt::scalar::sealed::OneValue. That helper trait is what keeps a missingZeroizevisible. A shorter name needs a decision on where the trait lives.sealnow walks the value throughEncryptRef::lend, not through&S. The two give the same walk,DEPTH, andFIXED. The tests compare the sealed leaf with the one aStringori64seals.cargo build, notcargo check, and only on a path that reachesseal.